package com.tiankun.eurekauserinfo.service.impl;

import com.tiankun.eurekauserinfo.exception.BusinessException;
import com.tiankun.eurekauserinfo.mapper.AttentionMapper;
import com.tiankun.eurekauserinfo.pojo.Attention;
import com.tiankun.eurekauserinfo.service.AttentionService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/11/12
 * Description
 * Version: V1.0
 */
@Service(value = "attentionService")
public class AttentionServiceImpl implements AttentionService {
    @Resource
    private AttentionMapper attentionMapper;
    Attention attention = new Attention();

    @Override
    public Integer add(Integer userId, Integer shopId) {
        //校验用户id和商品id是否为空
        if (userId == null || shopId == null) {
            throw new BusinessException("信息不全");
        }
        attention.setUserId(userId);
        attention.setShopId(shopId);
        //添加关注信息到数据库，并返回受影响行数
        int insert = attentionMapper.insert(attention);
        if (insert != 1) {
            throw new BusinessException("系统出错");
        }
        return insert;
    }

    @Override
    public Integer delete(Integer userId, Integer shopId) {

        //校验用户id和商品id是否为空
        if (userId == null || shopId == null) {
            throw new BusinessException("信息不全");
        }
        attention.setUserId(userId);
        attention.setShopId(shopId);
        //从数据库中删除关注信息（取消关注）
        //返回受影响行数
        return attentionMapper.delete(attention);
    }

    @Override
    public List<Attention> getList(Integer userId) {

        Example example = new Example(Attention.class);
        example.createCriteria().andEqualTo("userId", userId);

        return attentionMapper.selectByExample(example);

    }
}
